package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckGroupService;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 检查项服务
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService{
    @Autowired
    private CheckGroupDao checkGroupDao;

    @Override
    public Result add(CheckGroup checkGroup, Integer[] checkitemIds) {


        try {
            checkGroupDao.add(checkGroup);
            this.setCheckGroupAndChechItem(checkGroup.getId(), checkitemIds);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        if (queryString != null) {
            PageHelper.startPage(1, pageSize);
        } else {
            PageHelper.startPage(currentPage, pageSize);
        }

        Page<CheckGroup> checkGroups = checkGroupDao.selectByCondition(queryString);
        long total = checkGroups.getTotal();
        List<CheckGroup> result = checkGroups.getResult();
        return new PageResult(total, result);
    }

    @Override
    public Result findById(int id) {
        try {
            CheckGroup group = checkGroupDao.findById(id);
            if (group == null) {
                throw new RuntimeException();
            }
            List<Integer> checkItemIds = checkGroupDao.findCheckGroupIdAndCheckItemId(id);
            group.setCheckitemIds(checkItemIds);

            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS, group);
        } catch (Exception e) {
            return new Result(true, MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    @Override
    public List<Integer> findCheckGroupIdAndCheckItemId(int checkgroupId) {
        return checkGroupDao.findCheckGroupIdAndCheckItemId(checkgroupId);
    }

    @Override
    public Result edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        try {
            checkGroupDao.edit(checkGroup);
            //先删除对应的对应的检查项id
            Integer groupId = checkGroup.getId();
            checkGroupDao.deleteCheckGroupAndChechItemId(groupId);
            this.setCheckGroupAndChechItem(groupId, checkitemIds);

            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }

    @Override
    public Result deleteById( Integer id) {
        try {
            checkGroupDao.deleteCheckGroupAndChechItemId(id);
            checkGroupDao.deleteCheckGroup(id);
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @Override
    public Result finAll() {
        try {
            Page<CheckGroup> checkGroups = checkGroupDao.selectByCondition(null);
            return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkGroups);
        } catch (Exception e) {
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }

    private void setCheckGroupAndChechItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkGroupId", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupDao.addCheckGroupAndCheckItem(map);
            }
        }
    }


}
